package cn.yunhe.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;


public class MyFilter implements Filter {
	private String encoding;
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("过滤器在 初始化阶段 获取编码配置:"+filterConfig.getInitParameter("encoding"));
		this.encoding = filterConfig.getInitParameter("encoding");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		// TODO Auto-generated method stub
		System.out.println("第一个过滤器, 设置编码, 防止乱码发生");
		request.setCharacterEncoding(this.encoding);
		response.setCharacterEncoding(this.encoding);
		response.setContentType("text/html;charset="+this.encoding);
//		HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//		User user = (User) httpServletRequest.getSession().getAttribute("CURR_USER");
		/*if(user == null){
			request.getRequestDispatcher("userLogin.jsp").forward(request, response);
		}*/
		chain.doFilter(request, response);
		
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}
